package com.aracky.common.annotation.dataconvert.dict;

import com.aracky.common.annotation.DataDeal;
import com.aracky.common.mapper.BaseMapper;
import com.aracky.common.vo.DictVO;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zengzhiping
 * @ClassName DictUtil
 * @Description 字典转换表--查询字典数据
 * @createTime 2022年06月13日
 */
@Component
public class DictUtil {

    @Resource
    private BaseMapper baseMapper;

    @SneakyThrows
    public Boolean dealDict(List<DictDto> dictParams, Map<String, String> dictInfoMap, DataDeal annotation) {
        if (dictParams.size() == 0 || !annotation.dict()) {
            return Boolean.FALSE;
        }
        List<String> codes = getCodes(dictParams);
        //根据code 获取对应的字典内容------分布式需要调用接口
        List<DictVO> dictList = baseMapper.findDictList(codes);

        if (dictList == null || dictList.isEmpty()) {
            return Boolean.FALSE;
        }
        //先把字典值转成map
        for (DictVO dictVO : dictList) {
            dictInfoMap.put(dictVO.getCode() + "_" + dictVO.getValue(), dictVO.getName());
        }
        return Boolean.TRUE;
    }


    /**
     * 获取实体中所有需要翻译的code
     *
     * @param dictDtos
     * @return
     */
    private List<String> getCodes(List<DictDto> dictDtos) {
        if (dictDtos == null) {
            return new ArrayList<>();
        }
        List<String> collect = dictDtos.stream()
                .map(dictDto -> dictDto.getCode()).collect(Collectors.toList());
        collect.stream().distinct();
        return collect;
    }


}
